<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('到期预警')"/>
</head>
<body class="gray-bg" id="visit_id">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label for="periodSelect">过期时间：</label>
                            <select id="periodSelect" name="period">
                                <option value="6">一星期内</option>
                                <option value="29">一个月内</option>
                                <option value="59">两个月内</option>
                                <option value="89">三个月内</option>
                            </select>
                        </li>
                        <li>
                            <label for="isContainerPast">已过期：</label>
                            <select id="isContainerPast" name="isContainPast">
                                <option value="true">是</option>
                                <option value="false">否</option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    let communicationOperatorDatas = [[${@dict.getType('communication_operators')}]];
    let prefix = ctx + "workbench/channel_visit_log";

    $(loadTable)

    /**
     * 加载表格
     */
    function loadTable() {
        let options = {
            url: prefix + "/getAlmostExpireList",
            modalName: "到期提醒",
            showSearch: false,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'id',
                    title: 'id',
                    visible: false
                },
                {
                    field: 'wholeUnit.unit3.deptName',
                    title: '三级单元',
                    visible: false
                },
                {
                    field: 'wholeUnit.unit4.deptName',
                    title: '四级单元',
                    visible: false
                },
                {
                    field: 'wholeUnit.unit5.deptName',
                    title: '包区名称',
                    visible: false
                },
                {
                    field: 'wholeUnit.unit6.deptName',
                    title: '网格名称',
                    visible: false
                },
                {
                    field: 'wholeUnit.unit7.unitName',
                    title: '自然庄名称'
                },
                {
                    field: 'userName',
                    title: '用户姓名'
                },
                {
                    field: 'currentPhoneNumber',
                    title: '当前号码'
                },
                {
                    field: 'communicationOperator',
                    title: '用户类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(communicationOperatorDatas, value);
                    }

                },
                {
                    field: 'mealValue',
                    title: '套餐价值'
                },
                {
                    field: 'expiredTime',
                    title: '到期时间'
                },
                {
                    field: 'remake',
                    title: '备注',
                    formatter: function (value, row, index) {
                        if (value === null || value === '') {
                            return '无'
                        }
                        return value
                    }
                },
                {
                    field: 'createTime',
                    title: '上传日期'
                }]
        };
        $.table.init(options);
    }

    /**
     * 设置上级单元选择器
     * @param parentId 当前选择的项
     * @param level 单元等级
     */
    function setPreviousUnitSelect(parentId, level) {
        $.ajax({
            method: 'post',
            url: '/system/dept/selectByDeptId',
            data: {
                deptId: parentId
            },
            success: function (previousUnit) {
                let selectId
                switch (level) {
                    case 6:
                        selectId = "#unitSelect5"
                        setPreviousUnitSelect(previousUnit.parentId, 5)
                        break
                    case 5:
                        selectId = "#unitSelect4"
                        setPreviousUnitSelect(previousUnit.parentId, 4)
                        break
                    case 4:
                        selectId = "#unitSelect3"
                        break
                    case 3:
                        break
                    default:
                        console.log("单元等级错误p：" + level)
                }
                let option = "<option value='" + previousUnit.deptId + "'>" + previousUnit.deptName + "</option>"
                $(selectId).html(option).selectpicker('refresh');
            }
        })
    }

    /**
     * 设置当前选择器的值
     */
    function setCurrentSelect(dept, level) {
        let selectId
        switch (level) {
            case 6:
                selectId = "#unitSelect6"
                break
            case 5:
                selectId = "#unitSelect5"
                break
            case 4:
                selectId = "#unitSelect4"
                break
            case 3:
                selectId = "#unitSelect3"
                break
            case 2:
                return;
            default:
                console.log("单元等级错误c：" + level)
        }
        let options = "<option value='" + dept.deptId + "'>" + dept.deptName + "</option>";
        $(selectId).html(options).selectpicker('refresh');
    }

    /**
     * 设置下级单元选择器
     * @param deptId 本单元id
     * @param level 本单元等级
     */
    function setNextUnitSelect(deptId, level) {
        resetNextSelectByLevel(level)
        if (deptId === "") {
            return
        }
        $.ajax({
            method: 'post',
            url: '/system/dept/selectChildrenDeptById',
            data: {
                deptId: deptId,
                phone: 3
            },
            success: function (list) {
                let options = "<option value=\"\">所有</option>";
                for (let i = 0; i < list.length; i++) {
                    let option = list[i];
                    options += "<option value='" + option.deptId + "'>" + option.deptName + "</option>";
                }
                let selectId;
                let liId;
                switch (level) {
                    case 5:
                        selectId = "#unitSelect6";
                        liId = "#unitLi5";
                        setSelect7(list[0].deptId);
                        break;
                    case 4:
                        selectId = "#unitSelect5";
                        liId = "#unitLi4";
                        setNextUnitSelect(list[0].deptId, 5);
                        break;
                    case 3:
                        selectId = "#unitSelect4";
                        liId = "#unitLi3";
                        setNextUnitSelect(list[0].deptId, 4);
                        break;
                    case 2:
                        setNextUnitSelect(list[0].deptId, level + 1);
                        selectId = "#unitSelect3";
                        break;
                    default:
                        console.log("单元等级错误n：" + level)
                }
                $(selectId).html(options).selectpicker('refresh');
                if (liId !== '') {
                    $(liId).show();
                }
            }
        })
    }

    function setSelect7(unitId7) {
        $.ajax({
            method: 'post',
            url: '/workbench/leaf_unit/selectChildrenUnitByParentId',
            data: {
                parentId: unitId7
            },
            success: function (list) {
                let options = "<option value=\"\">所有</option>";
                for (let i = 0; i < list.length; i++) {
                    let option = list[i];
                    options += "<option value='" + option.unitId + "'>" + option.unitName + "</option>";
                }
                $("#unitSelect7").html(options).selectpicker('refresh');
            }
        })
    }

    /**
     * 设置选择器所有后，重置后续所有选择器未所有
     * @param level 单元等级
     */
    function resetNextSelectByLevel(level) {
        let options = "<option value=\"\">所有</option>"
        switch (level) {
            case 3:
                $("#unitSelect4").html(options).selectpicker('refresh')
                resetNextSelectByLevel(4)
                break
            case 4:
                $("#unitSelect5").html(options).selectpicker('refresh')
                resetNextSelectByLevel(5)
                break
            case 5:
                $("#unitSelect6").html(options).selectpicker('refresh')
                resetNextSelectByLevel(6)
                break
        }
    }

    /**
     * 如果选择自定义,显示日期选择框,否则隐藏
     * @param value value
     */
    function checkTimeLi(value) {
        if (value === '-1') {
            $("#startTimeLi").show()
            $("#endTimeLi").show()
        } else {
            $("#startTimeLi").hide()
            $("#endTimeLi").hide()
        }
    }
</script>
</body>
</html>
